<template>
    <div class="basicLayout">
        <div :class="['navSide', isFold ? 'fold' : 'unfold']">
            <div class="logo">
                <img src="../assets/logo.png" />
                <span>Manager</span>
            </div>
            <el-menu
                :default-active="1"
                class="navMenu"
                background-color="#001529"
                text-color="#fff"
                router
            >
                <TreeMenuVue :userMenu="userMenu"></TreeMenuVue>
            </el-menu>
        </div>
        <div :class="['contentRight', isFold ? 'fold' : 'unfold']">
            <div class="navTop">
                <div class="navLeft">
                    <div class="menuFold" @click="toggle">
                        <el-icon><Fold /></el-icon>
                    </div>
                    <div class="bread">
                        <BreadcrumbVue></BreadcrumbVue>
                    </div>
                </div>
                <div class="dropDownBox">
                    <el-dropdown @command="handlerLogout">
                        <span class="el-dropdown-link">
                            {{ userInfo.userName }}
                            <el-icon class="el-icon--right">
                                <arrow-down />
                            </el-icon>
                        </span>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item>邮箱：{{ userInfo.userEmail }}</el-dropdown-item>
                                <el-dropdown-item>退出</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </div>
            <div class="wrapper">
                <router-view></router-view>
            </div>
        </div>
    </div>
</template>

<script setup>
import TreeMenuVue from './TreeMenu.vue';
import BreadcrumbVue from './Breadcrumb.vue';
import api from '../api';
import { ref } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';

let userMenu = ref([]);
let isFold = ref(false);
const store = useStore();
const router = useRouter();
let userInfo = ref(store.state.userInfo);

const getPermissionList = async () => {
    let { actionList, menuList } = await api.getPermissionList();
    store.commit('saveMenuList', menuList);
    store.commit('saveActionList', actionList);
    userMenu.value = menuList;
};
getPermissionList();

const toggle = () => {
    isFold.value = !isFold.value;
};
const handlerLogout = key => {
    if (key == 'email') return;
    // 清除缓存 并 回到登录页
    store.commit('saveUserInfo', '');
    userInfo.value = {};
    router.push('/login');
};
</script>

<style lang="scss">
.basicLayout {
    position: relative;
    .navSide {
        position: fixed;
        width: 200px;
        height: 100vh;
        background-color: #001529;
        color: #fff;
        overflow: hidden;
        transition: width 0.5s;
        .logo {
            display: flex;
            align-items: center;
            font-size: 18px;
            height: 50px;
            img {
                margin: 0 16px;
                width: 32px;
                height: 32px;
            }
        }
        .navMenu {
            height: calc(100vh - 50px);
            border-right: none;
        }
        // 合并
        &.fold {
            width: 64px;
        }
        // 展开
        &.unfold {
            width: 200px;
        }
    }
    .contentRight {
        margin-left: 200px;
        // 合并
        &.fold {
            margin-left: 64px;
        }
        // 展开
        &.unfold {
            margin-left: 200px;
        }
        .navTop {
            height: 50px;
            line-height: 50px;
            display: flex;
            justify-content: space-between;
            border-bottom: 1px solid #ddd;
            padding: 0 20px;
            .navLeft {
                display: flex;
                align-items: center;
                .menuFold {
                    margin-right: 15px;
                    font-size: 18px;
                }
            }
            .userInfo {
                .notice {
                    line-height: 30px;
                    margin-right: 15px;
                    cursor: pointer;
                }
                .userLink {
                    cursor: pointer;
                    color: #409eff;
                }
            }
            .dropDownBox {
                display: flex;
                align-items: center;
                cursor: pointer;
            }
        }
        .wrapper {
            background: #eef0f3;
            padding: 20px;
            height: calc(100vh - 50px);
            .mainPage {
                background: #fff;
                height: 100%;
            }
        }
    }
}
</style>
